package config


import java.util.concurrent.{Executors, TimeUnit}

import dao.GrnaDao
import play.Logger

object GRNAClock extends MyAwait with MyFile {

  def grnaConfig(grnaDao: GrnaDao) = {
    val runnable = new Runnable {
      override def run() = {
        val time = System.currentTimeMillis()
        val grna = grnaDao.getAll.toAwait
        val clean = grna.filter(x => (time - x.time.toLong) / 1000.0 / 3600 / 24 > 30)
        clean.foreach{x=>
          s"${Global.path}/tmp/${x.file}".delete
        }
        grnaDao.deleteByIds(clean.map(_.file)).toAwait
        Logger.info("清理完成")
      }
    }

    val service = Executors.newSingleThreadScheduledExecutor()
    // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
    service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.DAYS)
  }


}
